<?php

App::uses('AppController', 'Controller');

/**
 * Logs Controller
 *
 * @property Log $Log
 */
class LogsController extends AppController {

    /**
     * index method
     *
     * @return void
     */
    public function index() {
        $this->Log->recursive = 0;
        $this->set('logs', $this->paginate());
    }

    public function admin_test() {
        debug("<br><br><br><br><br><br>");
        debug($this->params);
        $this->Log->recursive = 0;
        $this->set('logs', $this->paginate());
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        $this->Log->id = $id;
        if (!$this->Log->exists()) {
            throw new NotFoundException(__('Invalid data'));
        }
        $this->set('log', $this->Log->read(null, $id));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add() {
        if ($this->request->is('post')) {

            if (isset($this->request->data["Log"]["user_id"])) {
                $useId = AuthComponent::user('id');
                //$useId ="1";				
                $this->request->data["Log"]["user_id"] = $useId;
            }

            $this->Log->create();
            if ($this->Log->save($this->request->data)) {
                $id = $this->Log->id;
                $this->savelog($id);
                $this->Session->setFlash(__('Data has been saved'), 'default', array("class" => "mws-form-message success"));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('Data could not be saved. Please, try again.'), 'default', array("class" => "mws-form-message error"));
            }
        }
        $users = $this->Log->User->find('list');
        $this->set(compact('users'));
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        $this->Log->id = $id;
        if (!$this->Log->exists()) {
            throw new NotFoundException(__('Invalid log'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {

            if (isset($this->request->data["Log"]["user_id"])) {
                $useId = AuthComponent::user('id');
                //$useId ="1";				
                $this->request->data["Log"]["user_id"] = $useId;
            }

            if ($this->Log->save($this->request->data)) {
                $this->savelog($id);
                $this->Session->setFlash(__('Data has been saved'), 'default', array("class" => "mws-form-message success"));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('Data could not be saved. Please, try again.'), 'default', array("class" => "mws-form-message error"));
            }
        } else {
            $this->request->data = $this->Log->read(null, $id);
        }
        $users = $this->Log->User->find('list');
        $this->set(compact('users'));
    }

    /**
     * delete method
     *
     * @throws MethodNotAllowedException
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        if (!$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
        $this->Log->id = $id;
        if (!$this->Log->exists()) {
            throw new NotFoundException(__('Invalid log'));
        }
        if ($this->Log->delete()) {
            $this->savelog($id);

            $this->Session->setFlash(__('Data has been deleted'), 'default', array("class" => "mws-form-message success"));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Data was not deleted'), array('action' => 'index'), array('action' => 'index'));
        $this->redirect(array('action' => 'index'));
    }

}
